package com.bjut.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.DynaValidatorForm;

import com.bjut.action.base.BaseAction;
import com.bjut.domain.User;

public class LoginAction extends BaseAction {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		//DynaValidatorForm loginForm = (DynaValidatorForm)form;
		//System.out.println(""+loginForm.get("username"));	
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		System.out.println(username);	
		System.out.println(password);	
		User u=hrglxtService.login(username,password);
		ActionForward af; 
		if (u!=null) {
			request.getSession().setAttribute("USER_ID", u.getId());
			request.getSession().setAttribute("USER_NAME", u.getUsername());
			request.getSession().setAttribute("USER_TYPE", u.getPassword());
			if (request.getSession().getAttribute("PRE_URL") != null) {	
				String pre_url=""+ request.getSession().getAttribute("PRE_URL");
				System.out.println(pre_url);
				af= new ActionForward(pre_url,true);
			} else {
				af= mapping.findForward("loginSuccess");
			}
		} else {
			af= mapping.findForward("loginFailure");
		}
		request.setAttribute("msg", hrglxtService.getMsg());
		return af;
	}

}
